package com.dt.platform.hr.controller;

import java.io.File;
import java.util.*;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dt.platform.constants.enums.common.StatusValidEnum;
import com.dt.platform.constants.enums.hr.PaperQuestionTypeEnum;
import com.dt.platform.domain.hr.*;
import com.dt.platform.domain.hr.meta.PaperQuestionMeta;
import com.dt.platform.domain.hr.meta.PersonMeta;
import com.dt.platform.proxy.common.TplFileServiceProxy;
import com.github.foxnic.commons.bean.BeanUtil;
import com.github.foxnic.commons.log.Logger;
import org.apache.poi.ss.usermodel.Workbook;
import org.github.foxnic.web.framework.web.SuperController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import com.github.foxnic.commons.collection.CollectorUtil;
import com.github.foxnic.dao.entity.ReferCause;
import com.github.foxnic.api.swagger.InDoc;
import org.github.foxnic.web.framework.sentinel.SentinelExceptionUtil;
import com.github.foxnic.api.swagger.ApiParamSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dt.platform.proxy.hr.LearnPaperServiceProxy;
import com.dt.platform.domain.hr.meta.LearnPaperVOMeta;
import com.github.foxnic.api.transter.Result;
import com.github.foxnic.dao.data.SaveMode;
import com.github.foxnic.dao.excel.ExcelWriter;
import com.github.foxnic.springboot.web.DownloadUtil;
import com.github.foxnic.dao.data.PagedList;
import java.util.Date;
import java.sql.Timestamp;
import com.github.foxnic.api.error.ErrorDesc;
import com.github.foxnic.commons.io.StreamUtil;
import java.util.Map;
import com.github.foxnic.dao.excel.ValidateResult;
import java.io.InputStream;
import com.dt.platform.domain.hr.meta.LearnPaperMeta;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiImplicitParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dt.platform.hr.service.ILearnPaperService;
import com.github.foxnic.api.validate.annotations.NotNull;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 * 课程题库接口控制器
 * </p>
 * @author 金杰 , maillank@qq.com
 * @since 2024-09-14 11:34:59
 */
@InDoc
@Api(tags = "课程题库")
@RestController("HrLearnPaperController")
public class LearnPaperController extends SuperController {

    @Autowired
    private ILearnPaperService learnPaperService;

    /**
     * 添加课程题库
     */
    @ApiOperation(value = "添加课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "590431704064069"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NAME, value = "题库名称", required = false, dataTypeClass = String.class, example = "12"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NOTES, value = "备注", required = false, dataTypeClass = String.class, example = "1211212"),
		@ApiImplicitParam(name = LearnPaperVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TYPE, value = "题库类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TOTAL_CNT, value = "题目总数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.JUDGE_CNT, value = "判断题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.RADIO_CNT, value = "单选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.CHOICE_CNT, value = "多选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TEXTAREA_CNT, value = "填空题数", required = false, dataTypeClass = Integer.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true, ignorePrimaryKey = true)
    @ApiOperationSupport(order = 1, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnPaperServiceProxy.INSERT, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.INSERT)
    public Result insert(LearnPaperVO learnPaperVO) {
        Result result = learnPaperService.insert(learnPaperVO, false);
        return result;
    }

    /**
     * 验证题目
     */
    @ApiOperation(value = "删除验证题目课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "590431704064069")
	})
    @ApiOperationSupport(order = 2, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnPaperServiceProxy.VALID_QUESTION, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.VALID_QUESTION)
    public Result<JSONObject> validQuestion(String id) {
        return learnPaperService.validQuestion(id);
    }

    /**
     * 删除课程题库
     */
    @ApiOperation(value = "删除课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "590431704064069")
	})
    @ApiOperationSupport(order = 2, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnPaperServiceProxy.DELETE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.DELETE)
    public Result deleteById(String id) {
        this.validator().asserts(id).require("缺少id值");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 引用校验
        ReferCause cause = learnPaperService.hasRefers(id);
        // 判断是否可以删除
        this.validator().asserts(cause.hasRefer()).requireEqual("不允许删除当前记录：" + cause.message(), false);
        if (this.validator().failure()) {
            return this.validator().getFirstResult().messageLevel4Confirm();
        }
        Result result = learnPaperService.deleteByIdLogical(id);
        return result;
    }

    /**
     * 批量删除课程题库 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量删除课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnPaperServiceProxy.DELETE_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.DELETE_BY_IDS)
    public Result deleteByIds(List<String> ids) {
        // 参数校验
        this.validator().asserts(ids).require("缺少ids参数");
        if (this.validator().failure()) {
            return this.validator().getFirstResult();
        }
        // 查询引用
        Map<String, ReferCause> causeMap = learnPaperService.hasRefers(ids);
        // 收集可以删除的ID值
        List<String> canDeleteIds = new ArrayList<>();
        for (Map.Entry<String, ReferCause> e : causeMap.entrySet()) {
            if (!e.getValue().hasRefer()) {
                canDeleteIds.add(e.getKey());
            }
        }
        // 执行删除
        if (canDeleteIds.isEmpty()) {
            // 如果没有一行可以被删除
            return ErrorDesc.failure().message("无法删除您选中的数据行：").data(0).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                return e.hasRefer();
            }), ReferCause::message, String.class)).messageLevel4Confirm();
        } else if (canDeleteIds.size() == ids.size()) {
            // 如果全部可以删除
            Result result = learnPaperService.deleteByIdsLogical(canDeleteIds);
            return result;
        } else if (canDeleteIds.size() > 0 && canDeleteIds.size() < ids.size()) {
            // 如果部分行可以删除
            Result result = learnPaperService.deleteByIdsLogical(canDeleteIds);
            if (result.failure()) {
                return result;
            } else {
                return ErrorDesc.success().message("已删除 " + canDeleteIds.size() + " 行，但另有 " + (ids.size() - canDeleteIds.size()) + " 行数据无法删除").data(canDeleteIds.size()).addErrors(CollectorUtil.collectArray(CollectorUtil.filter(causeMap.values(), (e) -> {
                    return e.hasRefer();
                }), ReferCause::message, String.class)).messageLevel4Confirm();
            }
        } else {
            // 理论上，这个分支不存在
            return ErrorDesc.success().message("数据删除未处理");
        }
    }

    /**
     * 更新课程题库
     */
    @ApiOperation(value = "更新课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "590431704064069"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NAME, value = "题库名称", required = false, dataTypeClass = String.class, example = "12"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NOTES, value = "备注", required = false, dataTypeClass = String.class, example = "1211212"),
		@ApiImplicitParam(name = LearnPaperVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TYPE, value = "题库类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TOTAL_CNT, value = "题目总数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.JUDGE_CNT, value = "判断题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.RADIO_CNT, value = "单选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.CHOICE_CNT, value = "多选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TEXTAREA_CNT, value = "填空题数", required = false, dataTypeClass = Integer.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 4, author = "金杰 , maillank@qq.com", ignoreParameters = { LearnPaperVOMeta.PAGE_INDEX, LearnPaperVOMeta.PAGE_SIZE, LearnPaperVOMeta.SEARCH_FIELD, LearnPaperVOMeta.FUZZY_FIELD, LearnPaperVOMeta.SEARCH_VALUE, LearnPaperVOMeta.DIRTY_FIELDS, LearnPaperVOMeta.SORT_FIELD, LearnPaperVOMeta.SORT_TYPE, LearnPaperVOMeta.DATA_ORIGIN, LearnPaperVOMeta.QUERY_LOGIC, LearnPaperVOMeta.REQUEST_ACTION, LearnPaperVOMeta.IDS })
    @SentinelResource(value = LearnPaperServiceProxy.UPDATE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.UPDATE)
    public Result update(LearnPaperVO learnPaperVO) {
        Result result = learnPaperService.update(learnPaperVO, SaveMode.DIRTY_OR_NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 保存课程题库
     */
    @ApiOperation(value = "保存课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "590431704064069"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NAME, value = "题库名称", required = false, dataTypeClass = String.class, example = "12"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NOTES, value = "备注", required = false, dataTypeClass = String.class, example = "1211212"),
		@ApiImplicitParam(name = LearnPaperVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TYPE, value = "题库类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TOTAL_CNT, value = "题目总数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.JUDGE_CNT, value = "判断题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.RADIO_CNT, value = "单选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.CHOICE_CNT, value = "多选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TEXTAREA_CNT, value = "填空题数", required = false, dataTypeClass = Integer.class)
	})
    @ApiParamSupport(ignoreDBTreatyProperties = true, ignoreDefaultVoProperties = true)
    @ApiOperationSupport(order = 5, ignoreParameters = { LearnPaperVOMeta.PAGE_INDEX, LearnPaperVOMeta.PAGE_SIZE, LearnPaperVOMeta.SEARCH_FIELD, LearnPaperVOMeta.FUZZY_FIELD, LearnPaperVOMeta.SEARCH_VALUE, LearnPaperVOMeta.DIRTY_FIELDS, LearnPaperVOMeta.SORT_FIELD, LearnPaperVOMeta.SORT_TYPE, LearnPaperVOMeta.DATA_ORIGIN, LearnPaperVOMeta.QUERY_LOGIC, LearnPaperVOMeta.REQUEST_ACTION, LearnPaperVOMeta.IDS })
    @SentinelResource(value = LearnPaperServiceProxy.SAVE, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.SAVE)
    public Result save(LearnPaperVO learnPaperVO) {
        Result result = learnPaperService.save(learnPaperVO, SaveMode.DIRTY_OR_NOT_NULL_FIELDS, false);
        return result;
    }

    /**
     * 获取课程题库
     */
    @ApiOperation(value = "获取课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "1")
	})
    @ApiOperationSupport(order = 6, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnPaperServiceProxy.GET_BY_ID, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.GET_BY_ID)
    public Result<LearnPaper> getById(String id) {
        Result<LearnPaper> result = new Result<>();
        LearnPaper learnPaper = learnPaperService.getById(id);
        learnPaperService.dao().fill(learnPaper).with(LearnPaperMeta.TYPE_DICT).execute();
        result.success(true).data(learnPaper);
        return result;
    }

    /**
     * 批量获取课程题库 <br>
     * 联合主键时，请自行调整实现
     */
    @ApiOperation(value = "批量获取课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.IDS, value = "主键清单", required = true, dataTypeClass = List.class, example = "[1,3,4]")
	})
    @ApiOperationSupport(order = 3, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnPaperServiceProxy.GET_BY_IDS, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.GET_BY_IDS)
    public Result<List<LearnPaper>> getByIds(List<String> ids) {
        Result<List<LearnPaper>> result = new Result<>();
        List<LearnPaper> list = learnPaperService.queryListByIds(ids);
        result.success(true).data(list);
        return result;
    }

    /**
     * 查询课程题库
     */
    @ApiOperation(value = "查询课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "590431704064069"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NAME, value = "题库名称", required = false, dataTypeClass = String.class, example = "12"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NOTES, value = "备注", required = false, dataTypeClass = String.class, example = "1211212"),
		@ApiImplicitParam(name = LearnPaperVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TYPE, value = "题库类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TOTAL_CNT, value = "题目总数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.JUDGE_CNT, value = "判断题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.RADIO_CNT, value = "单选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.CHOICE_CNT, value = "多选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TEXTAREA_CNT, value = "填空题数", required = false, dataTypeClass = Integer.class)
	})
    @ApiOperationSupport(order = 5, author = "金杰 , maillank@qq.com", ignoreParameters = { LearnPaperVOMeta.PAGE_INDEX, LearnPaperVOMeta.PAGE_SIZE })
    @SentinelResource(value = LearnPaperServiceProxy.QUERY_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.QUERY_LIST)
    public Result<List<LearnPaper>> queryList(LearnPaperVO sample) {
        Result<List<LearnPaper>> result = new Result<>();
        List<LearnPaper> list = learnPaperService.queryList(sample);
        result.success(true).data(list);
        return result;
    }

    /**
     * 分页查询课程题库
     */
    @ApiOperation(value = "分页查询课程题库")
    @ApiImplicitParams({
		@ApiImplicitParam(name = LearnPaperVOMeta.ID, value = "主键", required = true, dataTypeClass = String.class, example = "590431704064069"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NAME, value = "题库名称", required = false, dataTypeClass = String.class, example = "12"),
		@ApiImplicitParam(name = LearnPaperVOMeta.NOTES, value = "备注", required = false, dataTypeClass = String.class, example = "1211212"),
		@ApiImplicitParam(name = LearnPaperVOMeta.UPDATE_BY, value = "修改人ID", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TYPE, value = "题库类型", required = false, dataTypeClass = String.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TOTAL_CNT, value = "题目总数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.JUDGE_CNT, value = "判断题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.RADIO_CNT, value = "单选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.CHOICE_CNT, value = "多选题数", required = false, dataTypeClass = Integer.class),
		@ApiImplicitParam(name = LearnPaperVOMeta.TEXTAREA_CNT, value = "填空题数", required = false, dataTypeClass = Integer.class)
	})
    @ApiOperationSupport(order = 8, author = "金杰 , maillank@qq.com")
    @SentinelResource(value = LearnPaperServiceProxy.QUERY_PAGED_LIST, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @PostMapping(LearnPaperServiceProxy.QUERY_PAGED_LIST)
    public Result<PagedList<LearnPaper>> queryPagedList(LearnPaperVO sample) {
        Result<PagedList<LearnPaper>> result = new Result<>();
        PagedList<LearnPaper> list = learnPaperService.queryPagedList(sample, sample.getPageSize(), sample.getPageIndex());
        learnPaperService.dao().fill(list).with(LearnPaperMeta.TYPE_DICT).execute();
        result.success(true).data(list);
        return result;
    }

    @SentinelResource(value = LearnPaperServiceProxy.IMPORT_QUESTION, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @RequestMapping(LearnPaperServiceProxy.IMPORT_QUESTION)
    public Result importExcel(MultipartHttpServletRequest request, HttpServletResponse response, String code,String id) throws Exception {
        // 获得上传的文件
        Map<String, MultipartFile> map = request.getFileMap();
        InputStream input = null;
        for (MultipartFile mf : map.values()) {
            input = StreamUtil.bytes2input(mf.getBytes());
            break;
        }
        if (input == null) {
            return ErrorDesc.failure().message("缺少上传的文件");
        }
        List<ValidateResult> errors = learnPaperService.importExcel(input, 0, code,id);
        if (errors == null || errors.isEmpty()) {
            return ErrorDesc.success();
        } else {
            Logger.info("import Result:");
            String msg = "导入失败";
            for (int i = 0; i < errors.size(); i++) {
                Logger.info(i + ":" + errors.get(i).message);
                msg = errors.get(i).message;
            }
            return ErrorDesc.failure().message(msg).data(errors);
        }
    }

    @SentinelResource(value = LearnPaperServiceProxy.EXPORT_QUESTION, blockHandlerClass = { SentinelExceptionUtil.class }, blockHandler = SentinelExceptionUtil.HANDLER)
    @RequestMapping(LearnPaperServiceProxy.EXPORT_QUESTION)
    public void exportExcel(String id, HttpServletResponse response) throws Exception {
        try {
            // 生成 Excel 数据
            InputStream inputstream = learnPaperService.buildExcelTemplate("hr_paper_question");
            if (inputstream == null) {
                return;
            }
            LearnPaper learnPaper = learnPaperService.getById(id);
            File f = TplFileServiceProxy.api().saveTempFile(inputstream, "tmp_hr_paper_question.xls");
            learnPaperService.dao().fill(learnPaper).with(LearnPaperMeta.PAPER_QUESTION_LIST).execute();

            List<PaperQuestion> list = learnPaper.getPaperQuestionList();
            learnPaperService.dao().fill(list).with(PaperQuestionMeta.LEVEL_DICT).with(PaperQuestionMeta.PAPER_QUESTION_ITEM_LIST).execute();
            Map<String, Object> map = new HashMap<>();
            List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
            for (int i = 0; i < list.size(); i++) {
                PaperQuestion paperQuestion = list.get(i);
                Map<String, Object> itemMap = new HashMap<>();
                itemMap.put("questionNumber", i + 1);
                if (PaperQuestionTypeEnum.RADIO.code().equals(paperQuestion.getQuestionType())) {
                    itemMap.put("questionType", "单选题");
                } else if (PaperQuestionTypeEnum.CHOICE.code().equals(paperQuestion.getQuestionType())) {
                    itemMap.put("questionType", "多选题");
                } else if (PaperQuestionTypeEnum.JUDGE.code().equals(paperQuestion.getQuestionType())) {
                    itemMap.put("questionType", "判断题");
                } else if (PaperQuestionTypeEnum.TEXTAREA.code().equals(paperQuestion.getQuestionType())) {
                    itemMap.put("questionType", "填空题");
                }
                itemMap.put("questionAnalysis", paperQuestion.getQuestionAnalysis());
                itemMap.put("levelName", paperQuestion.getLevelDict()==null?"":paperQuestion.getLevelDict().getLabel());
                if(StatusValidEnum.INVALID.code().equals(paperQuestion.getStatus())){
                    itemMap.put("questionStatusName",StatusValidEnum.INVALID.text());
                }else{
                    itemMap.put("questionStatusName",StatusValidEnum.VALID.text());
                }
                itemMap.put("questionName", paperQuestion.getName());
                itemMap.put("questionId", paperQuestion.getId());
                List<PaperQuestionItem> itemList = paperQuestion.getPaperQuestionItemList();
                JSONArray answerArr = new JSONArray();
                for (int j = 0; j < itemList.size(); j++) {
                    PaperQuestionItem item = itemList.get(j);
                    if ("right".equals(item.getSelected())) {
                        answerArr.add(item.getCode());
                    }
                    itemMap.put("item" + item.getCode(), item.getContent());
                }
                itemMap.put("answer", answerArr.toJSONString());
                itemMap.put("score", paperQuestion.getScore());
                listMap.add(itemMap);
            }
            map.put("dataList", listMap);
            TemplateExportParams templateExportParams = new TemplateExportParams(f.getPath());
            templateExportParams.setScanAllsheet(true);
            Workbook workbook = ExcelExportUtil.exportExcel(templateExportParams, map);
            DownloadUtil.writeToOutput(response, workbook, "题库.xls");
        } catch (Exception e) {
            DownloadUtil.writeDownloadError(response, e);
        }
    }
}
